<?php
require 'inc_security.php';
$cat_type = $array_type;
$list = new dataGrid('cat_id', 30);
$list->add('cat_name', 'Tên danh mục', 'string', 1, 1);
$list->add('cat_type', 'Loại danh mục', 'array', 0, 0);
$list->add('cat_order', 'Thứ tự', 'string', 0, 1);
$list->add('', 'Active', 'string');
$list->add('', 'Edit', 'edit');
$list->add('', 'Delete', 'delete');
$arrCat = $catBase->list_categories(0, 'cat_active = 1 ' . $list->sqlSearch(), 'cat_id,cat_parent_id,cat_order,cat_active,cat_name,cat_type', $list->sqlSort() . 'cat_id ASC');
$db_count = new db_count('SELECT count(*) as count
                            FROM ' . $bg_table . '
                            WHERE 1 ' . $list->sqlSearch() . '
                            ');
$total = $db_count->total;
unset($db_count);
$list->total_record = $total;
$total_row = $total;
$list_cat = array();
foreach ($arrCat as $i => $cat) {
    $tt = '';
    for ($j = 0; $j < $cat["level"]; $j++) $tt .= '| - - -  ';
    $list_cat[$cat["cat_id"]]['cat_name']      = $tt . $cat["cat_name"];
    $list_cat[$cat['cat_id']]['cat_type']      = $cat['cat_type'];
    $list_cat[$cat['cat_id']]['cat_order']     = $cat['cat_order'];
    $list_cat[$cat['cat_id']]['cat_active']    = $cat['cat_active'];
    $list_cat[$cat['cat_id']]['cat_parent_id'] = $cat['cat_parent_id'];
    $list_cat[$cat['cat_id']]['level']         = $cat['level'];
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#"
      xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <?= $load_header ?>
</head>
<body>
<div class="module_header bold fix"><?= $module_name ?></div>
<div id="wrapper">
    <?= $list->showHeader($total_row) ?>
    <?php
    $i = 0;
    ?>
    <?php foreach ($list_cat as $key => $value) {
        $i++;
        ?>
        <?
        if ($value['cat_parent_id'] == 0) {
            ?>
            <tr>
                <td style="background:#fafafa;text-align: left;font-weight: bold" colspan="9">
                    <?= 'Danh mục ' . $arr_type[$value['cat_type']] . ' : ' . $value['cat_name'] ?>
                </td>
            </tr>
        <?
        }
        ?>
        <?= $list->start_tr($i, $key) ?>
        <?
        switch ($value['level']) {
            case 2:
                $style_extra = 'font-style:italic;color:#777;';
                break;
            default:
                $style_extra = '';
                break;
        }?>
        <td><span class="span3" style="text-align:left;padding-left:30px;<?=$style_extra?>"><?= $value['cat_name'] ?></span></td>
        <td class="center"><?= $arr_type[$value['cat_type']] ?></td>
        <td class="center"><?= form_input('cat_order' . $key, $value['cat_order'], 'style="width:30px;text-align:center" onkeyup="check_edit(\'record_' . $i . '\')"') ?></td>
        <?= $list->showCheckbox('cat_active', $value['cat_active'], $key) ?>
        <?= $list->showEdit($key) ?>
        <?= $list->showDelete($key) ?>
        <?= $list->end_tr() ?>
    <? } ?>
    <?= $list->showFooter() ?>
</div>
</body>
</html>